 #include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <Stdbool.h>


 struct TreeNode {
      int val;
      struct TreeNode *left;
      struct TreeNode *right;
  };
bool _isBalanced(struct TreeNode* root,int* Depth)
{
    if(root==NULL)
    {
        *Depth=0;
        return true;
    }
    else
    {
        int leftDepth=0;
        if(_isBalanced(root->left,&leftDepth)==false)
            return false;

        int rightDepth=0;
        if(_isBalanced(root->right,&rightDepth)==false)
            return false;
        
        if(rightDepth-leftDepth>1||leftDepth-rightDepth>1)
            return false;
        
        *Depth=leftDepth>rightDepth?leftDepth+1:rightDepth+1;
        return true;
    }
}


bool isBalanced(struct TreeNode* root) {
 int size=0;
    return _isBalanced(root,&size); 	
}

